<template>
  <div class="main">
    <div class="buts"><el-button type="primary" style="width:120px;" @click="add()" size="small">新增</el-button></div>
     
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        type = index
        label="序号"
        width="60">
      </el-table-column>
      <el-table-column
        prop="date"
        label="日期"
        sortable
        width="120">
      </el-table-column>
      <el-table-column
        prop="name"
        label="活动名称"
        width="120">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址"
        width="240">
      </el-table-column>
      <el-table-column
        prop="delivery"
        label="是否配送"
        width="120">
        <template slot-scope="scope">
          {{scope.row.delivery == 1 ? '是' : '否'}}
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="活动性质"
        width="160">
        <template slot-scope="scope">
          <p v-for="(item,index) in scope.row.type" :key="index">{{item}}</p>
        </template>
      </el-table-column>
      <el-table-column
        prop="resource"
        label="特殊资源"
        width="120">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="200">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="primary" size="small">查看</el-button>
          <el-button type="primary" size="small"  @click="edit(scope.row)" >编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 模态框 -->
    <div class="shadow-model" v-if="addShow" @click="closeAdd()"> 
      <div class="el-icon-circle-close close" @click="closeAdd()"></div>
      <div class="add-w" @click="(e)=>{e.stopPropagation()}">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="活动名称">
            <el-input v-model="form.name" style="width:200px;"></el-input>
          </el-form-item>
          <el-form-item label="活动地址">
            <el-select v-model="form.address" placeholder="请选择活动地址" style="width:200px;">
              <el-option label="地址一" value="shanghai"></el-option>
              <el-option label="地址二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="活动时间">
              <el-date-picker type="date" placeholder="选择日期" v-model="form.date" style="width:200px;"></el-date-picker>
          </el-form-item>
          <el-form-item label="即时配送">
            <el-switch v-model="form.delivery"></el-switch>
          </el-form-item>
          <el-form-item label="活动性质">
            <el-checkbox-group v-model="form.type" @change="changeType">
              <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
              <el-checkbox label="地推活动" name="type"></el-checkbox>
              <el-checkbox label="线下主题活动" name="type"></el-checkbox>
              <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="特殊资源">
            <el-radio-group v-model="form.resource">
              <el-radio label="线上品牌商赞助"></el-radio>
              <el-radio label="线下场地免费"></el-radio>
            </el-radio-group>
          </el-form-item>
           <el-form-item label="編輯器">
              <editor-bar v-model="detail" :isClear="isClear" @change="change"></editor-bar>
           </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即创建</el-button>
            <el-button @click="addShow = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>


    <div class="shadow-model" v-if="editShow" @click="closeEdit()">
      <div class="el-icon-circle-close close" @click="closeEdit()"></div>
      <div class="add-w" @click="(e)=>{e.stopPropagation()}">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="活动名称">
            <el-input v-model="editForm.name" style="width:200px;"></el-input>
          </el-form-item>
          <el-form-item label="活动地址">
            <el-select v-model="editForm.address" placeholder="请选择活动地址" style="width:200px;">
              <el-option label="地址一" value="shanghai"></el-option>
              <el-option label="地址二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="活动时间">
              <el-date-picker type="date" placeholder="选择日期" v-model="editForm.date" style="width:200px;"></el-date-picker>
          </el-form-item>
          <el-form-item label="即时配送">
            <el-switch v-model="editForm.delivery"></el-switch>
          </el-form-item>
          <el-form-item label="活动性质">
            <el-checkbox-group v-model="editForm.type" @change="changeType">
              <el-checkbox label="美食/餐厅线上活动" name="type"></el-checkbox>
              <el-checkbox label="地推活动" name="type"></el-checkbox>
              <el-checkbox label="线下主题活动" name="type"></el-checkbox>
              <el-checkbox label="单纯品牌曝光" name="type"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-form-item label="特殊资源">
            <el-radio-group v-model="editForm.resource">
              <el-radio label="线上品牌商赞助"></el-radio>
              <el-radio label="线下场地免费"></el-radio>
            </el-radio-group>
          </el-form-item>
           <el-form-item>
            
            <el-button type="primary" @click="save(editForm)">保存</el-button>
            <el-button @click="editShow = false">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>


  </div>
</template>

<script>
import EditorBar  from '../components/wangEnduit.vue' // 引入富文本框
export default {
  name: 'home',
  data () {
    return {
      tableData: [{
        date: '2016-05-02',
        name: '活动名称1',
        address: '上海市普陀区金沙江路 1518 弄',
        delivery:1,
        type:['美食/餐厅线上活动'],
        resource:'线上品牌商赞助'
      }, {
        date: '2016-05-04',
        name: '活动名称2',
        address: '上海市普陀区金沙江路 1517 弄',
        delivery:0,
        type:['美食/餐厅线上活动','地推活动','线下主题活动','单纯品牌曝光'],
        resource:'线上品牌商赞助'
      }, {
        date: '2016-05-01',
        name: '活动名称3',
        address: '上海市普陀区金沙江路 1519 弄',
        delivery:1,
        type:['线下主题活动'],
        resource:'线下场地免费'
      }, {
        date: '2016-05-03',
        name: '活动名称4',
        address: '上海市普陀区金沙江路 1516 弄',
        delivery:1,
        type:['美食/餐厅线上活动','单纯品牌曝光'],
        resource:'线上品牌商赞助'
      }],
      addShow:false,
      form: {
        name: '',
        address: '',
        date: '',
        delivery: 0,
        type: [],
        resource: ''
      },
      editShow:false,
      editForm: {
      },
      //富文本框
      isClear: false,
      detail:""
    }
  },
  components: {
    EditorBar  // 引入富文本框
  },
  methods:{
    add(){
      this.addShow = true
      
    },
    closeAdd(){
      this.addShow = false
    },
    edit(row){
      this.editForm = row;
      this.editShow = true
    },
    closeEdit(){
      this.editShow = false;
    },
    onSubmit() {
      this.form.date = this.$moment(this.form.date).format('YYYY-MM-DD')
      this.tableData.push(this.form)
      this.closeAdd()
    },
     save(editForm) {
      this.form.date = this.$moment(this.form.date).format('YYYY-MM-DD')
      console.log(editForm.name)
      this.closeEdit()
    },
    handleClick(row) {
      console.log(row);
    },
    changeType(value){
      console.log(value)
    },
    change(val) {
      console.log(val)
    }
  },
  beforeCreate(){
    console.log("生命周期beforeCreate")
  },
  created(){
    console.log("生命周期created")
  },
  beforeMount(){
    console.log("生命周期beforeMount")
  },
  mounted(){
    console.log("生命周期mounted")
  },
  beforeUpdate(){
    console.log("生命周期beforeUpdate")
  },
  updated(){
    console.log("生命周期updated")
  },
  beforeDesotry(){
    console.log("生命周期beforeDesotry")
  },
  destoryed(){
    console.log("生命周期destoryed")
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
  @import "../css/home.scss"
</style>
